package com.china08.yxyapi.api.manage.edu.platform.lesson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudCatalogRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.service.lesson.TecCloudLessonService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class EduLessonListController extends YxyApiBaseController {

    @Autowired
    TecCloudLessonService lessonService;

    @Autowired
    YxyLessonsCloudCatalogRepository catalogRepository;

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/lesson/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String catalogId3 = reqModel.catalogId3;
        String catalogId4 = reqModel.catalogId4;
        String catalogId5 = reqModel.catalogId5;
        String status = reqModel.status;
        String keyword = reqModel.keyword;
        keyword = StringTrimUtil.trim(keyword);
        keyword = StringUtils.isNotBlank(keyword) ? "%" + keyword + "%" : "";
        Integer pageNum = reqModel.pageNum;
        String isJingPin = reqModel.isJingPin;
        // -1表示没有选择
        Integer jingPin = -1;
        if (StringUtils.isNotBlank(isJingPin) && ("1".equals(isJingPin) || "0".equals(isJingPin))) {
            jingPin = Integer.parseInt(isJingPin);
        }

        Integer allPage = 0;
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = lessonService.listForEdu(catalogId1, catalogId2, catalogId3, catalogId4, catalogId5, status, jingPin, keyword, pageNum);
        if (map != null) {
            allPage = Integer.parseInt(map.get("allPage") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }
        List<Pojo> returnList = new ArrayList<Pojo>();
        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            // 课程id
            String lessonId = m.get("id") + "";
            // 总课时
            Integer allCount = itemRepository.countByLessonId(lessonId);
            // 待审核课时数
            Integer itemStatus1Count = itemRepository.countByLessonIdAndStatus(lessonId, 1);
            // 已经通过课时
            Integer itemStatus2Count = itemRepository.countByLessonIdAndStatus(lessonId, 2);
            // 未通过课时数
            Integer itemStatus3Count = itemRepository.countByLessonIdAndStatus(lessonId, 3);
            pojo.allCount = allCount;
            pojo.itemStatus1Count = itemStatus1Count;
            pojo.itemStatus2Count = itemStatus2Count;
            pojo.itemStatus3Count = itemStatus3Count;
            pojo.id = lessonId;
            pojo.name = m.get("name") + "";
            pojo.cover = m.get("cover") + "";
            pojo.author = m.get("author") + "";
            if (m.get("school_nick") != null && !"null".equals(m.get("school_nick"))) {
                pojo.schoolNick = m.get("school_nick") + "";
            }
            String c1 = m.get("catalog_id1") + "";
            String c2 = m.get("catalog_id2") + "";
            String c3 = m.get("catalog_id3") + "";
            String c4 = m.get("catalog_id4") + "";
            String c5 = m.get("catalog_id5") + "";
            pojo.catalog1 = c1;
            pojo.catalog2 = c2;
            pojo.catalog3 = c3;
            pojo.catalog4 = c4;
            pojo.catalog5 = c5;
            // 获取栏目名称
            YxyLessonsCloudCatalog catalog = new YxyLessonsCloudCatalog();

            if (StringUtils.isNotBlank(c1) && !"null".equals(c1)) {
                catalog = catalogRepository.findById(c1);
                if (null != catalog) {
                    pojo.catalog1Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c2) && !"null".equals(c2)) {
                catalog = catalogRepository.findById(c2);
                if (null != catalog) {
                    pojo.catalog2Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c3) && !"null".equals(c3)) {
                catalog = catalogRepository.findById(c3);
                if (null != catalog) {
                    pojo.catalog3Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c4) && !"null".equals(c4)) {
                catalog = catalogRepository.findById(c4);
                if (null != catalog) {
                    pojo.catalog4Name = catalog.getName();
                }
            }
            if (StringUtils.isNotBlank(c5) && !"null".equals(c5)) {
                catalog = catalogRepository.findById(c5);
                if (null != catalog) {
                    pojo.catalog5Name = catalog.getName();
                }
            }
            Object obj0 = m.get("is_visible");
            Object obj1 = m.get("status");
            Object obj2 = m.get("count");
            Object obj3 = m.get("duration");
            Object obj4 = m.get("up_Date");
            Object obj5 = m.get("create_date");
            Object obj6 = m.get("update_date");
            Object obj8 = m.get("down_date");
            Object obj7 = m.get("vv");
            Object obj9 = m.get("audit_date");
            Object obj10 = m.get("is_jingpin_lesson");
            if (obj0 != null) {
                pojo.isVisible = Integer.parseInt(obj0 + "");
            }
            if (null != obj1) {
                pojo.status = Integer.parseInt(obj1 + "");
            }
            if (null != obj2) {
                pojo.count = Integer.parseInt(obj2 + "");
            }
            if (null != obj3) {
                Integer duration = Integer.parseInt(obj3 + "");
                String string = StringTrimUtil.disDuration(duration);
                pojo.duration = string;
            }
            if (null != obj4) {
                pojo.up_Date = DateToolUtils.format((Date) obj4, "yyyy-MM-dd");
            }
            if (null != obj5) {
                pojo.create_date = DateToolUtils.format((Date) obj5, "yyyy-MM-dd");
            }
            if (null != obj6) {
                pojo.update_date = DateToolUtils.format((Date) obj6, "yyyy-MM-dd");
            }
            if (null != obj7) {
                pojo.vv = Integer.parseInt(obj7 + "");
            }
            if (null != obj8) {
                pojo.down_Date = DateToolUtils.format((Date) obj8, "yyyy-MM-dd");
            }
            if (null != obj9) {
                pojo.auditDate = DateToolUtils.format((Date) obj9, "yyyy-MM-dd");
            }
            if (obj10 != null) {
                pojo.isJingpinLesson = Integer.parseInt(obj10 + "");
            }
            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.list = returnList;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String catalogId1;
        public String catalogId2;
        public String catalogId3;
        public String catalogId4;
        public String catalogId5;
        public String keyword;
        public String status;
        public Integer pageNum;
        public String isJingPin;
    }

    public static class Pojo {
        public String id;
        public String name;
        public String cover;
        public String catalog1 = "";
        public String catalog2 = "";
        public String catalog3 = "";
        public String catalog4 = "";
        public String catalog5 = "";
        public String catalog1Name = "";
        public String catalog2Name = "";
        public String catalog3Name = "";
        public String catalog4Name = "";
        public String catalog5Name = "";
        public String author = "";
        public Integer status = 0;
        public Integer isVisible = 0;
        public Integer isJingpinLesson = 0;
        public Integer count = 0;
        public Integer allCount = 0;
        public Integer itemStatus1Count;
        public Integer itemStatus2Count;
        public Integer itemStatus3Count;
        public Integer vv;
        public String duration = "";
        public String up_Date = "";
        public String down_Date = "";
        public String create_date = "";
        public String update_date = "";
        public String auditDate = "";
        public String schoolNick = "";
    }

    public static class RespModel extends YxyApiRespModel {
        public List<Pojo> list;
        public Integer allPage = 0;
    }
}
